package controller

import (
	"haiqiu-go-admin/result"
	"haiqiu-go-admin/service"
	"log"
	"net/http"
)

func (*NoticeApi) NoticeHandle(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodGet:
		NoticeAPI.noticeById(w, r)
		break
	case http.MethodPost:
		NoticeAPI.noticeAdd(w, r)
		break
	case http.MethodPut:
		NoticeAPI.noticeUpdate(w, r)
		break
	case http.MethodDelete:
		NoticeAPI.noticeDeleteBatch(w, r)
		break
	default:
		break
	}
}

// 根据ID查询数据
func (*NoticeApi) noticeById(w http.ResponseWriter, r *http.Request) {
	page, err := service.NoticeById(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// NoticePage 分页查询
func (*NoticeApi) NoticePage(w http.ResponseWriter, r *http.Request) {
	page, err := service.NoticePage(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// 新增数据
func (*NoticeApi) noticeAdd(w http.ResponseWriter, r *http.Request) {
	add, err := service.NoticeAdd(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 批量删除数据
func (*NoticeApi) noticeDeleteBatch(w http.ResponseWriter, r *http.Request) {
	add, err := service.NoticeDeleteBatch(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 根据ID更新数据
func (*NoticeApi) noticeUpdate(w http.ResponseWriter, r *http.Request) {
	add, err := service.NoticeUpdate(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// NoticeList 查询所有数据
func (*NoticeApi) NoticeList(w http.ResponseWriter, r *http.Request) {
	noticeList, err := service.NoticeList(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, noticeList)
}

func (*NoticeApi) Active(w http.ResponseWriter, r *http.Request) {
	active, err := service.ActiveNotice(w, r)
	if err != nil {
		log.Println(err.Error())
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, active)
}

func (*NoticeApi) Statistics(w http.ResponseWriter, r *http.Request) {
	mapData, err := service.StatisticsNotice()
	if err != nil {
		log.Println(err.Error())
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, mapData)
}
